package com.alex.interceptors;

import java.util.Properties;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

/**
 * @ClassName KafkaProducerInterceptor
 * @Description TODO
 * @Author zjg
 * @Date 2021/12/615:28
 * @Version 2.0
 */

public class KafkaProducerInterceptor {
    public static void main(String[] args) {
    // 创建KakfaProducer
      Properties props = new Properties();
      props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"49.233.172.9:9092");
      props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
      props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName());
      props.put(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG,UserDefineProducerInterceptor.class.getName());


      KafkaProducer<String, String> producer = new KafkaProducer<String, String>(props);


      for(int i =0;i<10;i++){
        ProducerRecord<String, String> record = new ProducerRecord<String, String>("topic01",
            "key" + i, "value" + i);
        producer.send(record);
      }
      producer.close();
    }
}
